package com.hilaver.dzmis.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.accesscontrol.AcAction;
import com.hilaver.dzmis.dao.LogDAO;
import com.hilaver.dzmis.log.Log;
import com.hilaver.dzmis.log.LogIp;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class LogServiceImpl extends AbstractBaseService implements
		IEntityService {
	
	public static String COLUMN_VISIT_DATE = "visitDate";
	
	public static String COLUMN_ACTION_NAME = "actionName";
	
	public static String COLUMN_WEEK = "week";
	
	public static String COLUMN_TIME = "time";

	private LogDAO dao;

	private AcUserServiceImpl auService;

	public LogServiceImpl() {
		this.dao = new LogDAO();
		this.auService = new AcUserServiceImpl();
	}

	@Override
	public String delete(int id) throws Exception {
		return super.delete(Log.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		return super.get(Log.class.getName(), id);
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		return super.getAllPagination(Log.class.getName(), page, sort, order);
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<Log> objList = this.dao.getAllPagination(Log.class.getName(),
				offset, Constants.PAGE_SIZE, sort, order, filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Log obj : objList) {
			obj = this.fillInLog(obj);

			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_VISIT_DATE + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_VISIT_DATE + ">");
			
			xmlSB.append("<" + COLUMN_WEEK + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate().getDay());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_WEEK + ">");
			
			xmlSB.append("<" + COLUMN_TIME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate().getHours());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_TIME + ">");
			
			xmlSB.append("<" + COLUMN_VISIT_DATE + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_VISIT_DATE + ">");
			
			if(obj.getAcUser() != null){
				xmlSB.append("<" + AcUserServiceImpl.COLUMN_REAL_NAME + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getAcUser().getRealName());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</" + AcUserServiceImpl.COLUMN_REAL_NAME + ">");
			}
			if(obj.getAcAction() != null){
				xmlSB.append("<" + COLUMN_ACTION_NAME + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				if(Constants.LOCALE_TYPE_ZH.equals(locale)){
					xmlSB.append(obj.getAcAction().getActionNameCn());
				}else if(Constants.LOCALE_TYPE_EN.equals(locale)){
					xmlSB.append(obj.getAcAction().getActionNameEn());
				}else{
					xmlSB.append(obj.getAcAction().getActionNameFr());
				}
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</" + COLUMN_ACTION_NAME + ">");
			}
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(Log.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getIllegalTimePagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<Log> objList = this.dao.getIllegalTimePagination(offset, Constants.PAGE_SIZE, sort, order, filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Log obj : objList) {
			obj = this.fillInLog(obj);

			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_VISIT_DATE + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_VISIT_DATE + ">");
			
			xmlSB.append("<" + COLUMN_WEEK + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate().getDay());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_WEEK + ">");
			
			xmlSB.append("<" + COLUMN_TIME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate().getHours());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_TIME + ">");
			
			if(obj.getAcUser() != null){
				xmlSB.append("<" + AcUserServiceImpl.COLUMN_REAL_NAME + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getAcUser().getRealName());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</" + AcUserServiceImpl.COLUMN_REAL_NAME + ">");
			}
			if(obj.getAcAction() != null){
				xmlSB.append("<" + COLUMN_ACTION_NAME + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				if(Constants.LOCALE_TYPE_ZH.equals(locale)){
					xmlSB.append(obj.getAcAction().getActionNameCn());
				}else if(Constants.LOCALE_TYPE_EN.equals(locale)){
					xmlSB.append(obj.getAcAction().getActionNameEn());
				}else{
					xmlSB.append(obj.getAcAction().getActionNameFr());
				}
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</" + COLUMN_ACTION_NAME + ">");
			}
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(Log.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getIllegalZonePagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<Log> objList = this.dao.getIllegalZonePagination(offset, Constants.PAGE_SIZE, sort, order, filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Log obj : objList) {
			obj = this.fillInLog(obj);

			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_VISIT_DATE + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_VISIT_DATE + ">");
			
			xmlSB.append("<" + COLUMN_WEEK + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate().getDay());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_WEEK + ">");
			
			xmlSB.append("<" + COLUMN_TIME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getVisitDate().getHours());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_TIME + ">");
			
			if(obj.getAcUser() != null){
				xmlSB.append("<" + AcUserServiceImpl.COLUMN_REAL_NAME + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getAcUser().getRealName());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</" + AcUserServiceImpl.COLUMN_REAL_NAME + ">");
			}
			if(obj.getAcAction() != null){
				xmlSB.append("<" + COLUMN_ACTION_NAME + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				if(Constants.LOCALE_TYPE_ZH.equals(locale)){
					xmlSB.append(obj.getAcAction().getActionNameCn());
				}else if(Constants.LOCALE_TYPE_EN.equals(locale)){
					xmlSB.append(obj.getAcAction().getActionNameEn());
				}else{
					xmlSB.append(obj.getAcAction().getActionNameFr());
				}
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</" + COLUMN_ACTION_NAME + ">");
			}
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(Log.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	private Log fillInLog(Log log) throws Exception {
		Boolean isNeed = false;
		if (log.getAcAction() == null) {
			isNeed = true;
			AcAction aa = this.auService.getActionByURI(log.getActionUrl());
			log.setAcAction(aa);
		}
		String ipAddress = log.getIpAddress();
		String[] ips = ipAddress.split("\\.");
		if (ips.length == 4 && log.getCountryCn() == null) {
			isNeed = true;
			Integer ipTotal = Integer.parseInt(ips[0]) * 256 * 256 * 256
					+ Integer.parseInt(ips[1]) * 256 * 256
					+ Integer.parseInt(ips[2]) * 256 + Integer.parseInt(ips[3]) - 1;
			List<LogIp> logIpList = this.dao.getLogIpFrom(ipTotal);
			if(logIpList.size() > 0){
				log.setCountryCn(logIpList.get(0).getCountry());
				log.setCityCn(logIpList.get(0).getCity());
			}
		}
		if(isNeed){
			this.dao.saveOrUpdate(log);
		}
		return log;
	}
	

	public static void main(String[] args) throws Exception {
		LogServiceImpl log = new LogServiceImpl();
		System.out.println(log.getAllPagination(1, "id", "asc", null));
	}
}
